package com.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exam.entity.FillQuestion;
import com.exam.entity.JudgeQuestion;
import com.exam.entity.MultiQuestion;
import com.exam.entity.PaperManage;
import com.exam.mapper.PaperManageMapper;
import com.exam.service.IFillQuestionService;
import com.exam.service.IJudgeQuestionService;
import com.exam.service.IMultiQuestionService;
import com.exam.service.IPaperManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷管理表 服务实现类
 * </p>
 *
 * @author zyh
 * @since 2023-03-09
 */
@Service
@Slf4j
public class PaperManageServiceImpl extends ServiceImpl<PaperManageMapper, PaperManage> implements IPaperManageService {

    @Autowired
    private IMultiQuestionService iMultiQuestionService;

    @Autowired
    private IFillQuestionService iFillQuestionService;

    @Autowired
    private IJudgeQuestionService iJudgeQuestionService;



    @Override
    @Transactional
    public Map<Integer, List<?>> findPaper(Integer paperId) {
        List<PaperManage> PaperManage=this.list(new QueryWrapper<PaperManage>().eq("paperId",paperId));
        List<Integer> PaperMulti = new ArrayList<>();
        List<Integer> PaperFill = new ArrayList<>();
        List<Integer> PaperJudge = new ArrayList<>();
        for (PaperManage p:PaperManage) {
            if (p.getQuestionType()==1){
                PaperMulti.add(p.getQuestionId());
            }else if (p.getQuestionType()==2){
                PaperFill.add(p.getQuestionId());
            } else if (p.getQuestionType()==3) {
                PaperJudge.add(p.getQuestionId());
            }
        }
        List<MultiQuestion> multiQuestions = iMultiQuestionService.list(new QueryWrapper<MultiQuestion>().in("questionId", PaperMulti));
        log.info("multiQuestions:{}",multiQuestions);
        List<FillQuestion> fillQuestions = iFillQuestionService.list(new QueryWrapper<FillQuestion>().in("questionId", PaperFill));
        log.info("fillQuestions:{}",fillQuestions);
        List<JudgeQuestion> judgeQuestions = iJudgeQuestionService.list(new QueryWrapper<JudgeQuestion>().in("questionId", PaperJudge));
        log.info("judgeQuestions:{}",judgeQuestions);
        Map<Integer, List<?>> map = new HashMap<>();
        map.put(1,multiQuestions);
        map.put(2,fillQuestions);
        map.put(3,judgeQuestions);
        return  map;
    }
}
